﻿using AutoMapper;
using B.W.passport.Application.DTO;
using B.W.passport.Domain;
using B.W.passport.IBaseRepisitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.W.passport.Application.breedService
{
    public class BreedService:IBreedService
    {
        private readonly IBaseRepisitory<Breed> BreedRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="breedRepository"></param>
        /// <param name="mapper"></param>
        public BreedService(IBaseRepisitory<Breed> breedRepository, IMapper mapper)
        {
            BreedRepository = breedRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="breed"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> AddBreed(BreedDTO breed)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnumsCode.添加成功;
            result.Message = APIEnumsCode.添加成功.ToString();

            var list =_mapper.Map<Breed>(breed);
            await BreedRepository.AddAsync(list);

            return result;
        }

        /// <summary>
        /// 获取所有养殖信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<Breed>> GetBreed(int id)
        {
            APIResult<Breed> result = new APIResult<Breed>();
            result.Code = APIEnumsCode.查询成功;
            result.Message = APIEnumsCode.查询成功.ToString();

            var list = BreedRepository.GetAll().ToList();
            var list1 = list.FirstOrDefault(p => p.BreedId == id);

            result.Data = list1;

            return result;
        }

        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> UpdateBreed(BreedDTO breed)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIEnumsCode.修改成功;
            result.Message = APIEnumsCode.修改成功.ToString();
            // var list = ProductRepisitory.Find(x => x.ProductId == product.ProductId);
            var list = _mapper.Map<Breed>(breed);
            BreedRepository.UpdateDataAsync(list);

            return await Task.FromResult(result);
        }
    }
}
